package com.jiuyou.clientController.mall.mallAddress;

import cn.hutool.core.lang.Dict;
import com.jiuyou.clientController.base.ClientBaseController;
import com.ruoyi.common.constant.DicConstants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.shopping.domain.MallAddress;
import com.ruoyi.shopping.service.IMallAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: chengliang
 * @Date: Created in 2022/6/22
 */
@RestController
@RequestMapping("/client/mall/address")
public class MallAddressClientController extends ClientBaseController {
    @Autowired
    private IMallAddressService addressService;

    @PostMapping
    public TableDataInfo getList(){
        MallAddress mallAddress = new MallAddress();
        mallAddress.setCreateBy(getLoginMemberId());
        return getDataTable(addressService.selectMallAddressList(mallAddress));
    }
    @PostMapping("/getDefault")
    public MallAddress getDefault(){
        MallAddress mallAddress = new MallAddress();
        mallAddress.setCreateBy(getLoginMemberId());
        mallAddress.setDefaultFlag(DicConstants.SYS_Y.getCode());
        List<MallAddress> list = addressService.selectMallAddressList(mallAddress);
        return list.size()>0?list.get(0):null;
    }
    @PostMapping("/getOne/{id}")
    public MallAddress getOne(@PathVariable Long id){
        return addressService.selectMallAddressById(id);
    }

    @PostMapping("/save")
    public Integer add(MallAddress mallAddress){
        mallAddress.setCreateBy(getLoginMemberId());
        if(DicConstants.SYS_Y.getCode().equals(mallAddress.getDefaultFlag())){
            MallAddress c = new MallAddress();
            c.setCreateBy(getLoginMemberId());
            c.setDefaultFlag(DicConstants.SYS_N.getCode());
            addressService.updateByCreate(c);
        }
        return addressService.insertMallAddress(mallAddress);
    }
    @PostMapping("/edit")
    public Integer edit(@RequestBody MallAddress mallAddress){
        mallAddress.setCreateBy(getLoginMemberId());
        if(DicConstants.SYS_Y.getCode().equals(mallAddress.getDefaultFlag())){
            MallAddress c = new MallAddress();
            c.setCreateBy(getLoginMemberId());
            c.setDefaultFlag(DicConstants.SYS_N.getCode());
            addressService.updateByCreate(c);
        }
        return addressService.updateMallAddress(mallAddress);
    }
    @PostMapping("/setDefault/{id}")
    public AjaxResult setDefault(@PathVariable Long id){
        MallAddress mallAddress = new MallAddress();
        mallAddress.setCreateBy(getLoginMemberId());
        mallAddress.setDefaultFlag(DicConstants.SYS_N.getCode());
        addressService.updateByCreate(mallAddress);
        return AjaxResult.success(addressService.setDefaultFlag(id, DicConstants.SYS_Y.getCode()));
    }
    @PostMapping("/resetDefault/{id}")
    public AjaxResult resetDefault(@PathVariable Long id){
        return AjaxResult.success(addressService.setDefaultFlag(id, DicConstants.SYS_N.getCode()));
    }
    @PostMapping("/del/{id}")
    public AjaxResult del(@PathVariable Long id){
        return AjaxResult.success(addressService.deleteMallAddressById(id));
    }
    @PostMapping("/getDetail/{id}")
    public AjaxResult getDetail(@PathVariable Long id){
        return AjaxResult.success(addressService.selectMallAddressById(id));
    }

}
